package com.lyshop.jinxiang.adapters;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.lyshop.jinxiang.R;
import com.lyshop.jinxiang.base.BaseRVAdapter;
import com.lyshop.jinxiang.base.BaseRVHolder;
import com.lyshop.jinxiang.base.BaseViewHolder;
import com.lyshop.jinxiang.bean.OrderInfoBean;
import com.lyshop.jinxiang.utils.GlideUtils;
import com.zc.mychart.utils.ArithUtil;
import com.zc.mychart.utils.FormatUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class OrderRecordRVAdapter extends BaseRVAdapter<OrderInfoBean> {
    private SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm:ss", Locale.CHINA);

    public OrderRecordRVAdapter(Context context, List<OrderInfoBean> list) {
        super(context, list);
    }

    @Override
    public int onCreateViewLayoutID(int viewType) {
        return R.layout.item_order_record_layout;
    }

    @Override
    public void onBindView(BaseRVHolder mHolder, final int position) {
        BaseViewHolder holder = mHolder.getViewHolder();

        RelativeLayout rl_title = holder.get(R.id.rl_title);
        ImageView iv_goods_img = holder.get(R.id.iv_goods_img);
        TextView tv_pro_name = holder.get(R.id.tv_pro_name);
        TextView tv_pro_number = holder.get(R.id.tv_pro_number);
        final ImageView iv_arrow = holder.get(R.id.iv_arrow);
        ImageView iv_trade_type = holder.get(R.id.iv_trade_type);
        final LinearLayout ll_record_info = holder.get(R.id.ll_record_info);
        ImageView iv_money_type = holder.get(R.id.iv_money_type);
        TextView tv_profit_loss_text = holder.get(R.id.tv_profit_loss_text);
        TextView tv_describe_01 = holder.get(R.id.tv_describe_01);
        TextView tv_describe_02 = holder.get(R.id.tv_describe_02);
        TextView tv_describe_03 = holder.get(R.id.tv_describe_03);
        TextView tv_describe_04 = holder.get(R.id.tv_describe_04);
        TextView tv_describe_05 = holder.get(R.id.tv_describe_05);
        TextView tv_describe_06 = holder.get(R.id.tv_describe_06);
        TextView tv_describe_07 = holder.get(R.id.tv_describe_07);
        TextView tv_describe_08 = holder.get(R.id.tv_describe_08);
        TextView tv_describe_09 = holder.get(R.id.tv_describe_09);
        TextView tv_describe_10 = holder.get(R.id.tv_describe_10);

        final OrderInfoBean bean = mList.get(position);

        GlideUtils.showImageRoundOptions(mContext, bean.product_img, iv_goods_img, 12);

        if (!bean.isExpand) {
            ll_record_info.setVisibility(View.GONE);
            iv_arrow.setImageResource(R.drawable.icon_arrow_gray_down);
        } else {
            ll_record_info.setVisibility(View.VISIBLE);
            iv_arrow.setImageResource(R.drawable.icon_arrow_gray_up);
        }

        rl_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRecordListener.onClickExpand(position);
            }
        });

        ll_record_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRecordListener.onCLickDetail(bean);
            }
        });

        //商品内容
        tv_pro_name.setText(bean.product_name);
        if (bean.trade_type == 2) {
            iv_trade_type.setImageResource(R.drawable.img_home_up);
        } else {
            iv_trade_type.setImageResource(R.drawable.img_home_down);
        }
        iv_money_type.setVisibility(bean.use_ticket == 1 ? View.VISIBLE : View.GONE);
        //商品详情
        double result = Double.parseDouble(bean.pro_loss);

        if (result >= 0) {
            tv_profit_loss_text.setTextColor(mContext.getResources().getColor(R.color.buy_red));
            tv_profit_loss_text.setText(String.format("+%s元", FormatUtil.formatMoney(result)));
        } else {
            tv_profit_loss_text.setTextColor(mContext.getResources().getColor(R.color.buy_green));
            tv_profit_loss_text.setText(String.format("%s元", FormatUtil.formatMoney(result)));
        }
        String number = " x " + bean.amount + "件";
        tv_pro_number.setText(number);


        String type = "";
        switch (bean.liqui_type) {
            case 3:
                type = "手动退订";
                break;
            case 4:
                type = "止盈退订";
                break;
            case 5:
                type = "止损退订";
                break;
            case 6:
                type = "协议退订";
                break;
            case 7:
                type = "系统退订";
                break;
        }
        //商品具体信息
        String orderPrice = "订购价：" + FormatUtil.formatDouble2(bean.build_price);
        String buyTime = "下单时间：" + dateFormat.format(new Date(Long.parseLong(bean.build_time)));
        String sellPrice = "退订价：" + FormatUtil.formatDouble2(bean.liqui_price);
        String sellTime = "退订时间：" + dateFormat.format(new Date(Long.parseLong(bean.liqui_time)));
        String floatDot;
        double dotNumber = ArithUtil.sub(bean.liqui_price, bean.build_price);
        dotNumber = dotNumber * bean.multiple;
        if (dotNumber >= 0) {
            floatDot = "浮动点：上升" + FormatUtil.formatDouble2(dotNumber) + "点";
        } else {
            floatDot = "浮动点：下降" + FormatUtil.formatDouble2(Math.abs(dotNumber)) + "点";
        }
        String target = "止盈/止损：" + (bean.target_profit == 0 ? "不限" : bean.target_profit) + "/" + (bean.stop_loss == 0 ? "不限" : bean.stop_loss + "点");
        String orderMoney = "订金：" + FormatUtil.formatDouble2(ArithUtil.mul(bean.unit_price, bean.amount)) + "元";
        String tradeFee = "服务费：" + FormatUtil.formatNumberForOne(bean.trade_fee) + "元";
        String moneyType = "购买方式：" + (bean.use_ticket == 1 ? "代金券" : "账户余额");
        String sellType = "退订类型：" + type;

        tv_describe_01.setText(orderPrice);
        tv_describe_02.setText(buyTime);
        tv_describe_03.setText(sellPrice);
        tv_describe_04.setText(sellTime);
        tv_describe_05.setText(floatDot);
        tv_describe_06.setText(target);
        tv_describe_07.setText(orderMoney);
        tv_describe_08.setText(tradeFee);
        tv_describe_09.setText(moneyType);
        tv_describe_10.setText(sellType);
    }


    private RecordListener mRecordListener;

    public void setRecordListener(RecordListener listener) {
        this.mRecordListener = listener;
    }

    public interface RecordListener {
        void onClickExpand(int position);

        void onCLickDetail(OrderInfoBean bean);
    }

}
